package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoq.mapper.OrderItemMapper;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.OrderItem;
import com.guoq.pojo.OrderItemExample;
import com.guoq.pojo.OrderItemExample.Criteria;
import com.guoq.pojo.Result;
import com.guoq.service.IOrderItemService;

/**
 * 
 * <P>类名称:OrderItemServiceImpl
 * <P>类描述:  订单商品项service
 * <P>创建人:郭强  
 * <P>修改人:XXX 
 * <P>修改时间:2017年7月22日 下午4:23:46
 * <P>修改备注: 
 * @version 1.0.0
 * Copyright(c)2017西部在线-版权所有
 */
@Service("orderItemService")
public class OrderItemServiceImpl implements IOrderItemService {

	@Resource
	private OrderItemMapper orderItemMapper;

	/**
	 * 根据id查询商品信息
	 */
	@Override
	public OrderItem getById(int id) {
		// TODO Auto-generated method stub
		return this.orderItemMapper.selectByPrimaryKey(id);
	}

	/**
	 * 根据订单号获取不分页列表
	 */
	@Override
	public List<OrderItem> getListByOrderid(String order_id) {
		
		// TODO Auto-generated method stub
		OrderItemExample example = new OrderItemExample();
		Criteria criteria = example.createCriteria();
		example.setOrderByClause("createtime desc");
		List<OrderItem> list = orderItemMapper.selectByExample(example);
		return list;
	}

	/**
	 * 根据商品id获取分页列表
	 */
	@Override
	public EUDataGridResult getPageListByItemid(Integer page, Integer rows, String item_id) {
		
		OrderItemExample example = new OrderItemExample();
		Criteria criteria = example.createCriteria();
		//分页处理
		PageHelper.startPage(page, rows);
		example.setOrderByClause("createtime desc");
		
		List<OrderItem> list = orderItemMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<OrderItem> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	/**
	 * 根据订单id获取分页列表
	 */
	@Override
	public EUDataGridResult getPageListByOrderid(Integer page, Integer rows, String order_id) {
		
		OrderItemExample example = new OrderItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(order_id);
		//分页处理
		PageHelper.startPage(page, rows);
		example.setOrderByClause("createtime desc");
		
		List<OrderItem> list = orderItemMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<OrderItem> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	@Override
	public List<OrderItem> selectByExample(OrderItemExample example) {
		// TODO Auto-generated method stub
		List<OrderItem> list = orderItemMapper.selectByExample(example);
		return list;
	}

	@Override
	public OrderItem selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		OrderItem record = orderItemMapper.selectByPrimaryKey(id);
		return record;
	}

	@Override
	public int countByExample(OrderItemExample example) {
		// TODO Auto-generated method stub
		int count = orderItemMapper.countByExample(example);
		return count;
	}

	@Override
	public Result create(OrderItem record) throws Exception {

		Result result = this.insert(record);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 添加商品描述
	 *<P>开发者:郭强
	 *<P>方法名: insertItemDesc
	 *<P>描述: 
	 *@param itemId
	 *@param desc
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	private Result insert(OrderItem record){
		
		record.setCreatetime(new Date());
		record.setUpdatetime(new Date());
		orderItemMapper.insertSelective(record);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(OrderItem record) throws Exception {

		Result result = null;
		
		record.setUpdatetime(new Date());
		int num = orderItemMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result update(OrderItem record) throws Exception {

		Result result = null;
		int num = orderItemMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	@Override
	public List<OrderItem> getListByItemid(String item_id) {
		// TODO Auto-generated method stub
		return null;
	}

}
